package me.yushuai.wukong.base.repository;

import me.yushuai.wukong.base.entity.Conf;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Optional;

/**
 * BAS_CONF_ITEM
 *
 * @author zhoushuai@189.cn
 * @since 2022-09-13
 */
@Repository
public interface ConfRepository extends JpaRepository<Conf, Long>, JpaSpecificationExecutor<Conf> {

    @Modifying
    @Query("delete from Conf where groupId = ?1 and id = ?2")
    void deleteById(Long groupId, Long confId);

    @Modifying
    @Query("delete from Conf where groupId = ?1")
    void deleteByGroupId(Long groupId);

    @Query("from Conf i, ConfGroup b where  i.groupId = b.groupId and i.bizId = ?2 and b.bizId = ?1")
    Optional<Conf> findByBizId(String groupBizId, String confBizId);

    @Query("from Conf where id =?2 and groupId = ?1")
    Optional<Conf> findById(Long groupId, Long confId);

//    @Query("select count(id) > 0 from Conf where id = ?1 and bizId = ?2")
    Boolean existsByGroupIdAndBizId(Long groupId, String confBizId);

//    @Query("select count(id) > 0 from Conf where id = ?2 and groupId =?1")
    Boolean existsByGroupIdAndId(Long groupId, Long confId);
}




